package lib.bone.system.service.impl;

import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.system.dao.SysRoleDAO;
import lib.bone.system.dao.SysUserDAO;
import lib.bone.system.domain.entity.SysRole;
import lib.bone.system.domain.entity.SysUser;
import lib.bone.system.service.SysUserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author : kindear
 * @Date : 2021-12-13
 */
@Service
@Slf4j
public class SysUserRoleServiceImpl implements SysUserRoleService {
//    @Autowired
//    SysUserRoleDAO sysUserRoleDAO;
    @Autowired
    SysRoleDAO sysRoleDAO;
    @Autowired
    SysUserDAO sysUserDAO;

    @Override
    public List<SysRole> getUserRoles(Long userId) {
        Optional<SysUser> userOp = sysUserDAO.findById(userId);
        if(!userOp.isPresent()){
            log.warn("传入ID:{}不存在对应信息",userId);
            return new ArrayList<>();
        }
        Set<SysRole> roleSet = userOp.get().getRoles();
        return new ArrayList<>(roleSet);
    }

    @Override
    public List<SysUser> getRoleUsers(Long roleId) {

        Optional<SysRole> roleOp =sysRoleDAO.findById(roleId);
        if(!roleOp.isPresent()){
            log.warn("传入ID:{}不存在对应信息",roleId);
            return new ArrayList<>();
        }
        log.info(roleOp.get().getUsers().toString());
        Set<SysUser> userSet = roleOp.get().getUsers();
        return new ArrayList<>(userSet);
    }

    @Override
    @Transactional
    public ResponseDTO grantRoleUsers(Long roleId,List<Long> userIds){
        //        Optional<SysRole> sysRoleOp = sysRoleDAO.findById(roleId);
//        if(!sysRoleOp.isPresent())
//            return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND);
////        List<SysUser> sysUserList = sysUserDAO.findByIdIn(userIds);
//        SysRole sysRole = sysRoleOp.get();
//        Set<SysUser> userSet = sysRole.getUsers();
//
//        return ResponseDTO.succMsg("更新成功!");
//    }
        Optional<SysRole> sysRoleOp = sysRoleDAO.findById(roleId);
        if(!sysRoleOp.isPresent()) {
            return ResponseDTO.wrap(ResponseCodeConst.ERROR_PARAM);
        }
        SysRole sysRole = sysRoleOp.get();
//        List<SysUser> existUsers = new ArrayList<>(sysRole.getUsers());
//        existUsers.stream().map(sysUser -> {
//            sysUser.getRoles().remove(sysRole);
//            return sysUser;
//        }).collect(Collectors.toList());
//        sysUserDAO.saveAll(existUsers);
        List<SysUser> sysUserList = sysUserDAO.findByIdIn(userIds);
        sysUserList.stream().map(sysUser -> {
            sysUser.getRoles().add(sysRole);
            return sysUser;
        }).collect(Collectors.toList());
        sysUserDAO.saveAll(sysUserList);
        return ResponseDTO.succMsg("授权成功!");
    }
    @Override
    @Transactional
    public ResponseDTO cancelRoleUsers(Long roleId,List<Long> userIds){
        Optional<SysRole> sysRoleOp = sysRoleDAO.findById(roleId);
        if(!sysRoleOp.isPresent())
            return ResponseDTO.wrap(ResponseCodeConst.ERROR_PARAM);
        SysRole sysRole = sysRoleOp.get();
        List<SysUser> sysUserList = sysUserDAO.findByIdIn(userIds);
        sysUserList.stream().map(sysUser -> {
            sysUser.getRoles().remove(sysRole);
            return sysUser;
        }).collect(Collectors.toList());
        sysUserDAO.saveAll(sysUserList);
        return ResponseDTO.succMsg("取消授权成功!");
    }

    @Override
    @Transactional
    public ResponseDTO grantUserRole(Long userId, Long roleId) {
        Optional<SysUser> userOp = sysUserDAO.findById(userId);
        Optional<SysRole> roleOp = sysRoleDAO.findById(roleId);
        if(roleOp.isPresent() && userOp.isPresent()){
            SysUser sysUser = userOp.get();
            sysUser.getRoles().add(roleOp.get());
            sysUserDAO.save(sysUser);
            return ResponseDTO.succMsg("为用户新增角色成功");
        }else {
            return ResponseDTO.wrap(ResponseCodeConst.ERROR_PARAM);
        }
    }

    @Override
    @Transactional
    public ResponseDTO  grantUserRoles(Long userId,List<Long> roleIds){
        List<SysRole> sysRoleList = sysRoleDAO.findByIdIn(roleIds);
        Optional<SysUser> userOp = sysUserDAO.findById(userId);
        if(!userOp.isPresent())
            return ResponseDTO.wrap(ResponseCodeConst.ERROR_PARAM);
        SysUser sysUser = userOp.get();
        sysUser.setRoles(new HashSet<>(sysRoleList));
        sysUserDAO.save(sysUser);
        return ResponseDTO.succMsg("为用户新增角色成功");
    }

    @Override
    @Transactional
    public ResponseDTO grantRoleUser(Long roleId, Long userId) {
        Optional<SysUser> userOp = sysUserDAO.findById(userId);
        Optional<SysRole> roleOp = sysRoleDAO.findById(roleId);
        if(roleOp.isPresent() && userOp.isPresent()){
            SysUser sysUser = userOp.get();
            sysUser.getRoles().add(roleOp.get());
            sysUserDAO.save(sysUser);
            return ResponseDTO.succMsg("为角色新增用户成功");
        }else {
            return ResponseDTO.wrap(ResponseCodeConst.ERROR_PARAM);
        }

    }

    @Override
    @Transactional
    public ResponseDTO cancelRoleUser(Long roleId,Long userId){
        Optional<SysUser> userOp = sysUserDAO.findById(userId);
        Optional<SysRole> roleOp = sysRoleDAO.findById(roleId);
        if(roleOp.isPresent() && userOp.isPresent()){
            SysUser sysUser = userOp.get();
            sysUser.getRoles().remove(roleOp.get());
            sysUserDAO.save(sysUser);
            return ResponseDTO.succMsg("角色删除用户成功");
        }else {
            return ResponseDTO.wrap(ResponseCodeConst.ERROR_PARAM);
        }
    }

    /**
     * 为角色批量授权用户
     */
//    @Transactional
//    public ResponseDTO batchAuthRoleUser(Long roleId,List<Long> userIds){
//        Optional<SysRole> sysRoleOp = sysRoleDAO.findById(roleId);
//        if(!sysRoleOp.isPresent())
//            return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND);
////        List<SysUser> sysUserList = sysUserDAO.findByIdIn(userIds);
//        SysRole sysRole = sysRoleOp.get();
//        Set<SysUser> userSet = sysRole.getUsers();
//
//        return ResponseDTO.succMsg("更新成功!");
//    }
}
